<template>
  <div class="app-container">
    <el-form label-width="70px" :inline="true" size="small">
      <el-form-item label="关 键 字">
        <el-input style="width: 95%" v-model="searchObj.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="操作时间">
        <el-date-picker
          v-model="createTimes"
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData()">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetData">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%;margin-top: 10px;"
    >

      <el-table-column
        label="序号"
        width="70"
        align="center"
      >
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="username" align="center" label="用户账号"/>
      <el-table-column prop="ipaddr" align="center" label="登陆IP"/>

      <el-table-column label="登陆状态" align="center">
        <template slot-scope="scope">
          <el-button type="text" v-if="scope.row.status ===0">成功</el-button>
          <el-button type="text" v-if="scope.row.status ===1">失败</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="msg" align="center" label="提示信息"/>
      <el-table-column prop="createTime" align="center" label="创建时间"/>

    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      style="padding: 30px 0; text-align: right;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

  </div>
</template>

<script>
import api from '@/api/system/log'

export default {
  // 定义数据
  data() {
    return {
      listLoading: false, // 数据是否正在加载
      list: null, // 列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      searchObj: {}, // 查询表单对象

      createTimes: []
    }
  },

  // 当页面加载时获取数据
  created() {
    this.fetchData()
  },

  methods: {

    fetchData(page = 1) {
      this.page = page
      if (this.createTimes && this.createTimes.length === 2) {
        this.searchObj.createTimeBegin = this.createTimes[0]
        this.searchObj.createTimeEnd = this.createTimes[1]
      }

      api.getPageList(this.page, this.limit, this.searchObj)
        .then(response => {
          this.list = response.data.records
          this.total = response.data.total
        })
    },

    resetData() {
      this.searchObj = {}
      this.createTimes = []
      this.fetchData()
    }

  }
}
</script>
